package com.jinlejia.bbs.ui.fun;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.TagBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.databinding.BbsActivityInvitedViewListBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.InviterecordLogic;
import com.jinlejia.bbs.ui.reply.BBSReplyDetailActivity;
import com.jinlejia.bbs.widget.BBSToast;

import java.util.ArrayList;

public class BBSInvitedViewListActivity extends AppCompatActivity implements CommonEvent.BackFilter{
    private BbsActivityInvitedViewListBinding binding;
    private BBSUserBean bean;
    private String topicCode;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.bbs_activity_invited_view_list);
        bean = getIntent().getParcelableExtra("user");
        topicCode = getIntent().getStringExtra("topicCode");
        binding.setUser(bean);
        binding.setCommonEvent(CommonEvent.obtain());
        context = this;
        initView();
    }

    private void initView() {
        ArrayList<TagBean> tags = bean.getTags();
        if (tags!=null){
            binding.tagGroupFlexboxLayout2.removeAllViews();
            for (TagBean tb:tags) {
                TextView textView = new TextView(this);
                textView.setTextColor(Color.parseColor("#FCCB00"));
                textView.setText(tb.getValueName());
                textView.setBackgroundResource(R.drawable.bbs_shape_search_tag_bg2);
                textView.setTextSize(10);
                binding.tagGroupFlexboxLayout2.addView(textView);
            }
        }
        binding.rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context,BBSChooseTagActivity.class);
                BBSInvitedViewListActivity.this.startActivityForResult(intent,10);

            }
        });
        binding.submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InviterecordLogic logic = new InviterecordLogic(context);
                logic.execute(bean.getUserPin(), bean.getUserName(), topicCode, new InviterecordLogic.LogicCallBack() {
                    @Override
                    public void onResult(boolean result) {
                        if (result){
                            BBSToast.showToast(context,"邀请成功",1500);
                            setResult(RESULT_OK);
                            Intent intent = new Intent();
                            TopicBean topic = new TopicBean();
                            topic.setTopicCode(topicCode);
                            intent.putExtra("topic",topic);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent.setClass(context, BBSReplyDetailActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            Intent intent = new Intent();
            intent.setClass(context,BBSInvitedUserListActivity.class);
            intent.putExtra("tag",data.getStringExtra("data"));
            intent.putExtra("topicCode",topicCode);
            startActivity(intent);
        }
    }

    @Override
    public boolean onBack() {
        Intent intent = new Intent();
        TopicBean topic = new TopicBean();
        topic.setTopicCode(topicCode);
        intent.putExtra("topic",topic);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(context, BBSReplyDetailActivity.class);
        startActivity(intent);
        return false;
    }
}
